using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIMain : MonoBehaviour
{
    public Image playerhp;
    public Image hpBuffer;
    public Text hpPercent;
    public Image playerExp;
    public Text expPercent;

    public GameObject panelPause;
    public GameObject panelOver;
    public GameObject panelMission;    

    public GameObject panelLoad;
    public Text text;
    public Slider load;
    public Button Handover;

    public GameObject pause;
    public GameObject over;

    public GameObject bag;

    public PlayerInputHandler player;

    public GameObject backSpace;

    private bool isClike;
    private bool isEsc;
    private void Start()
    {
        panelPause.SetActive(false);
        panelOver.SetActive(false);
        panelLoad.SetActive(false);
        panelMission.SetActive(false);  
        over.SetActive(false);
        bag.SetActive(false);
        backSpace.SetActive(false);  
        isClike = true;
        Handover.interactable = false;
        Time.timeScale = 1;
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            isClike = !isClike;
            isEsc = !isEsc; 
        }

        if (isEsc == false)
        {
            if (panelPause.activeSelf || panelOver.activeSelf || panelMission.activeSelf || bag.activeSelf)
            {
                isClike = false;
            }
            else isClike = true;
        }

        if (Input.GetKeyUp(KeyCode.E))
        {
            OpenorCloseBag();
        }

        player.OnApplicationFocus(isClike);

        if (hpBuffer.fillAmount > playerhp.fillAmount)
        {
            hpBuffer.fillAmount -= 0.5f * Time.deltaTime;
        }
        else hpBuffer.fillAmount = playerhp.fillAmount;

    }


    public void PlayHp(float v)
    {
        playerhp.fillAmount = v;
        hpPercent.text = (v * 100).ToString("#0.00") + "%";
        if (v <= 0.3f)
        {
            playerhp.color = Color.red; 
        }
        else if (v <= 0.5)
        {
            playerhp.color= Color.yellow;
        }
        else playerhp.color= Color.green;
    }
    public void PlayExp(float v)
    {
        playerExp.fillAmount = v;
        expPercent.text = (v * 100).ToString("#0.00") + "%";
    }


    public void GamePause()
    {
        Time.timeScale = 0;
        isClike = false;
        pause.SetActive(false);
        over.SetActive(true);
        panelPause.SetActive(true);
        
    }

    public void GameOver()
    {
        panelOver.SetActive(true);
    }

    public void GameContinue()
    {
        Time.timeScale = 1;
        pause.SetActive(true);
        over.SetActive(false);
        panelPause.SetActive(false);
    }

    public void GameSave()
    {
        GameController.instance.Save();
    }

    public void GameReturn()
    {
        panelPause.SetActive(false);
        panelLoad.SetActive(true);
        StartCoroutine(LoadScene());
    }

    public void OpenorCloseBag()
    {
        bag.SetActive(!bag.activeSelf);
    }

    public void ChangebackSpace(int v)
    {
        if (v > 1 && backSpace.activeSelf == false)
        {
            backSpace.SetActive(true);
        }
        else backSpace.SetActive(false);
    }

    public void OpenorCloseMission()
    {
        panelMission.SetActive(!panelMission.activeSelf);
    }

    IEnumerator LoadScene()
    {
        yield return null;
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("GameMenu");
        asyncOperation.allowSceneActivation = false;
        while (!asyncOperation.isDone)
        {
            text.text = asyncOperation.progress * 100f + "%";
            load.value = 1f * asyncOperation.progress;
            Handover.interactable = asyncOperation.progress >= 0.9f;
            if (asyncOperation.progress >= 0.9f)
            {
                load.value = 1;
                text.text = 100 + "%";
                Handover.onClick.AddListener(() => asyncOperation.allowSceneActivation = true);
            }
            yield return null;
        }
    }
}
